home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / programming / emacs-complete / fsf / emacs / lisp / flow-ctrl.el < prev    next >
Lisp/Scheme  |  1994-05-28  |  5KB  |  119 lines

  1. ;;; flow-ctrl.el --- help for lusers on cu(1) or ttys with wired-in ^S/^Q flow control
  2.  
  3. ;;; Copyright (C) 1990, 1991, 1994 Free Software Foundation, Inc.
  4.  
  5. ;; Author Kevin Gallagher
  6. ;; Maintainer: FSF
  7. ;; Adapted-By: ESR
  8. ;; Keywords: hardware
  9.  
  10. ;; This file is part of GNU Emacs.
  11.  
  12. ;; GNU Emacs is free software; you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation; either version 2, or (at your option)
  15. ;; any later version.
  16.  
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. ;; GNU General Public License for more details.
  21.  
  22. ;; You should have received a copy of the GNU General Public License
  23. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  24. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  25.  
  26. ;;; Commentary:
  27.  
  28. ;;;; Terminals that use XON/XOFF flow control can cause problems with
  29. ;;;; GNU Emacs users.  This file contains Emacs Lisp code that makes it
  30. ;;;; easy for a user to deal with this problem, when using such a
  31. ;;;; terminal. 
  32. ;;;;      
  33. ;;;; To invoke these adjustments, a user need only invoke the function
  34. ;;;; enable-flow-control-on with a list of terminal types in his/her own
  35. ;;;; .emacs file.  As arguments, give it the names of one or more terminal
  36. ;;;; types in use by that user which require flow control adjustments.
  37. ;;;; Here's an example: 
  38. ;;;; 
  39. ;;;;    (enable-flow-control-on "vt200" "vt300" "vt101" "vt131")
  40.  
  41. ;;; Portability note: This uses (getenv "TERM"), and therefore probably
  42. ;;; won't work outside of UNIX-like environments.
  43.  
  44. ;;; Code:
  45.  
  46. (defvar flow-control-c-s-replacement ?\034
  47.   "Character that replaces C-s, when flow control handling is enabled.")
  48. (defvar flow-control-c-q-replacement ?\036
  49.   "Character that replaces C-q, when flow control handling is enabled.")
  50.  
  51. ;;;###autoload
  52. (defun enable-flow-control (&optional argument)
  53.   "Toggle flow control handling.
  54. When handling is enabled, user can type C-s as C-\\, and C-q as C-^.
  55. With arg, enable flow control mode if arg is positive, otherwise disable."
  56.   (interactive "P")
  57.   (if (if argument
  58.       ;; Argument means enable if arg is positive.
  59.       (<= (prefix-numeric-value argument) 0)
  60.     ;; No arg means toggle.
  61.     (nth 1 (current-input-mode)))
  62.       (progn
  63.     ;; Turn flow control off, and stop exchanging chars.
  64.     (set-input-mode t nil (nth 2 (current-input-mode)))
  65.     (if keyboard-translate-table
  66.         (progn
  67.           (aset keyboard-translate-table flow-control-c-s-replacement
  68.             flow-control-c-s-replacement)
  69.           (aset keyboard-translate-table ?\^s ?\^s)
  70.           (aset keyboard-translate-table flow-control-c-q-replacement
  71.             flow-control-c-q-replacement)
  72.           (aset keyboard-translate-table ?\^q ?\^q))))
  73.     ;; Turn flow control on.
  74.     ;; Tell emacs to pass C-s and C-q to OS.
  75.     (set-input-mode nil t (nth 2 (current-input-mode)))
  76.     ;; Initialize translate table, saving previous mappings, if any.
  77.     (let ((the-table (make-string 128 0)))
  78.       (let ((i 0)
  79.         (j (length keyboard-translate-table)))
  80.     (while (< i j)
  81.       (aset the-table i (elt keyboard-translate-table i))
  82.       (setq i (1+ i)))
  83.     (while (< i 128)
  84.       (aset the-table i i)
  85.       (setq i (1+ i))))
  86.       (setq keyboard-translate-table the-table))
  87.     ;; Swap C-s and C-\
  88.     (aset keyboard-translate-table flow-control-c-s-replacement ?\^s)
  89.     (aset keyboard-translate-table ?\^s flow-control-c-s-replacement)
  90.     ;; Swap C-q and C-^
  91.     (aset keyboard-translate-table flow-control-c-q-replacement ?\^q)
  92.     (aset keyboard-translate-table ?\^q flow-control-c-q-replacement)
  93.     (message (concat 
  94.           "XON/XOFF adjustment for " 
  95.           (getenv "TERM") 
  96.           ":  use C-\\ for C-s  and  use C-^ for C-q."))
  97.     (sleep-for 2)))            ; Give user a chance to see message.
  98.  
  99. ;;;###autoload
  100. (defun enable-flow-control-on (&rest losing-terminal-types)
  101.   "Enable flow control if using one of a specified set of terminal types.
  102. Use `(enable-flow-control-on \"vt100\" \"h19\")' to enable flow control
  103. on VT-100 and H19 terminals.  When flow control is enabled,
  104. you must type C-\\ to get the effect of a C-s, and type C-^
  105. to get the effect of a C-q."
  106.   (let ((term (getenv "TERM"))
  107.     hyphend)
  108.     (if term
  109.     (progn
  110.       ;; Strip off hyphen and what follows
  111.       (while (setq hyphend (string-match "[-_][^-_]+$" term))
  112.         (setq term (substring term 0 hyphend)))
  113.       (and (member term losing-terminal-types)
  114.            (enable-flow-control))))))
  115.  
  116. (provide 'flow-ctrl)
  117.  
  118. ;;; flow-ctrl.el ends here
  119.